#!/bin/bash
#!/usr/bin/env bash
usage() {
    pname=$(basename $0)
    echo "$pname --operat <operat type> --domain_path <domain path> --run_user <run user> --listen_port <adminserver listen port> --timeout <timeout>"
    exit -1
}

parseOpts() {
    OPT_SPEC=":h-:"
    while getopts "$OPT_SPEC" optchar; do
        case "${optchar}" in
        -)
            case "${OPTARG}" in
            operat)
                operat="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            run_user)
                run_user="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            domain_path)
                domain_path="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            listen_port)
                listen_port="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            timeout)
                timeout="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            *)
                if [ "$OPTERR" = 1 ] && [ "${OPT_SPEC:0:1}" != ":" ]; then
                    echo "Unknown option --${OPTARG}" >&2
                fi
                ;;
            esac
            ;;
        h)
            usage
            exit 2
            ;;
        *)
            if [ "$OPTERR" != 1 ] || [ "${OPT_SPEC:0:1}" = ":" ]; then
                echo "Non-option argument: '-${OPTARG}'" >&2
            fi
            ;;
        esac
    done
}

parseOpts "$@"

if [[  ! -n "$domain_path" ]] ; then
    echo "ERROR:: Must defined domain_path."
    usage
fi 

if [[  ! -n "$run_user" ]] ; then
    echo "ERROR:: Must defined run_user."
    usage
fi 

if [[  ! -n "$listen_port" ]] ; then
    echo "ERROR:: Must defined listen_port."
    usage
fi 

if [[ ! -n "$operat" ]] ; then 
    operat="start"
fi 

if [[ ! -n "$timeout" ]] ; then 
    timeout=60
fi 

temp_file="/tmp/weblogic.log"
port=`netstat -anp|grep $listen_port`
pinfo=`ps -ef | grep weblogic | grep "AdminServer" | grep -v grep | awk '{print $2}'`
if [[ "$operat" == "start" ]] ; then 
    if [[ -n "$port" && -n "$pinfo" ]]; then
        echo "FINE:: Weblogic AdminServer already start."
        exit 0
    else 
        su - "$run_user" -c "nohup $domain_path/bin/startWebLogic.sh > $temp_file 2>&1 &"
    fi 
else
    if [[ ! -n "$port" && ! -n "$pinfo"  ]]; then
        echo "FINE:: Weblogic AdminServer already stop."
        exit 0
    else 
        su - "$run_user" -c "nohup $domain_path/bin/stopWebLogic.sh > $temp_file 2>&1 &"
    fi 
fi

if [ $? != 0  ] ;then 
    echo "Weblogic AdminServer $operat failed.$@"
    exit 1
fi 

loopcount=$((timeout/5))
exitCode=1
if [[ "$operat" == "start" ]] ; then 
    while [ $exitCode != 0 ]
    do
        loopcount=$((loopcount-1))
        port=`netstat -anp|grep $listen_port`
        if [ -n "$port" ]; then
            exitCode=0
            tail -n 200 $temp_file
            echo "FINE:: Start Weblogic AdminServer success."
            rm -f $temp_file
        else
            echo "INFO:: Start Weblogic AdminServer Running..."
        fi
        sleep 5

        if [[ $loopcount == 0 ]] ; then 
            tail -n 200 $temp_file
            echo "ERROR:: Start Weblogic AdminServer failed."
            exitCode=2
            break
        fi
    done
    exit $exitCode
else 
    while [ $exitCode != 0 ]
    do
        loopcount=$((loopcount-1))
        pid=`ps -ef | grep weblogic |grep AdminServer |awk '{print $2}'`
        if [ ! -n "$pid" ]; then
            exitCode=0
            tail -n 200 $temp_file
            echo "FINE:: Stop Weblogic AdminServer success."
            rm -f $temp_file
        else
            echo "INFO:: Stop Weblogic AdminServer Running..."
        fi
        sleep 5

        if [[ $loopcount == 0 ]] ; then 
            kill -9 $pid
            tail -n 200 $temp_file
            echo "FINE:: Stop Weblogic AdminServer success."
            exitCode=0
            break
        fi
    done
    exit $exitCode
fi 
